from flask import Flask, render_template, request
import json,time
from db import DB
from unit import Unit
from layout import Layout
from battle import Battle
from item import UseItem

app = Flask(__name__)

def wlog():
    f = open(app.root_path + "/conn.log", "a")
    f.write("%s 连接自：%s, url:%s\n"%(time.ctime(), request.remote_addr, request.url))
    f.flush()
    f.close()

@app.route("/")
def index():
    wlog()
    return render_template("index.html")


@app.route("/main")
def main():
    wlog()
    return render_template("main.html")


@app.route("/api/get_hero")
def get_hero():
    wlog()
    hero = Unit("英雄", 1, 0, 100, 200, 10, 5, 60, 5,
                5, 100, 500, 10, 5, 1, 1, 1, "1", "")

    return hero.r()


@app.route("/api/get_map")
def get_map():
    wlog()
    floor = request.args.get("floor")
    level = request.args.get("level")
    layout = Layout(int(floor), int(level))

    return json.dumps(layout.map), 200, {"content_type": 'application/json'}


@app.route("/api/battle", methods=['GET', 'POST'])
def battle():
    wlog()
    hero = {
        "skill": request.form.get("hero[skill]"),
        "equipment": request.form.get("hero[equipment]"),
        "atk": request.form.get("hero[atk]"),
        "cri": request.form.get("hero[cri]"),
        "defense": request.form.get("hero[defense]"),
        "dod": request.form.get("hero[dod]"),
        "exp": request.form.get("hero[exp]"),
        "hit": request.form.get("hero[hit]"),
        "hp": request.form.get("hero[hp]"),
        "level": request.form.get("hero[level]"),
        "level_atk": request.form.get("hero[level_atk]"),
        "level_defense": request.form.get("hero[level_defense]"),
        "level_dod": request.form.get("hero[level_dod]"),
        "level_exp": request.form.get("hero[level_exp]"),
        "level_hit": request.form.get("hero[level_hit]"),
        "level_hp": request.form.get("hero[level_hp]"),
        "max_exp": request.form.get("hero[max_exp]"),
        "name": request.form.get("hero[name]"),
    }
    monster_id = request.form.get("monster")

    player = Unit(**hero)
    monster = Unit(**DB().get_monster(int(monster_id)))
    battle_info = Battle(player, monster)
    return battle_info.r()


@app.route("/api/useItem", methods=['GET', 'POST'])
def useItem():
    wlog()
    hero = {
        "skill": request.form.get("hero[skill]"),
        "equipment": request.form.get("hero[equipment]"),
        "atk": request.form.get("hero[atk]"),
        "cri": request.form.get("hero[cri]"),
        "defense": request.form.get("hero[defense]"),
        "dod": request.form.get("hero[dod]"),
        "exp": request.form.get("hero[exp]"),
        "hit": request.form.get("hero[hit]"),
        "hp": request.form.get("hero[hp]"),
        "level": request.form.get("hero[level]"),
        "level_atk": request.form.get("hero[level_atk]"),
        "level_defense": request.form.get("hero[level_defense]"),
        "level_dod": request.form.get("hero[level_dod]"),
        "level_exp": request.form.get("hero[level_exp]"),
        "level_hit": request.form.get("hero[level_hit]"),
        "level_hp": request.form.get("hero[level_hp]"),
        "max_exp": request.form.get("hero[max_exp]"),
        "name": request.form.get("hero[name]"),
    }

    item_id = request.form.get("item")

    player = Unit(**hero)

    return UseItem(player).r(int(item_id))


if __name__ == "__main__":
    app.run(debug=True)
